package com.dr.springboot.starter.handler;

import com.dr.springboot.starter.VO.ResultVO;
import com.dr.springboot.starter.enums.ResultEnum;
import com.dr.springboot.starter.exception.MVCException;
import com.dr.springboot.starter.exception.RestException;
import com.dr.springboot.starter.util.ResultVOUtil;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * Created by rui on 2018/7/25.
 */
//todo exception 创建同一个的异常处理，依据不同的异常类型来进行处理
@ControllerAdvice
public class TestExceptionHandler {

//    todo exception 如果需要返回的是一个json对象，则需要加上ResponseBody
    @ExceptionHandler(value = RestException.class)
    @ResponseBody
    public ResultVO ajaxHandle(HttpServletRequest request, Exception e) {
        return ResultVOUtil.error(ResultEnum.ERROR);
    }

    @ExceptionHandler(value = MVCException.class)
    public ModelAndView mvcHandle(HttpServletRequest request, Exception e) {
        HashMap map = new HashMap();
        map.put("message", "内部错误");
        return new ModelAndView("freemarker/error", map);
    }
}
